package nowcoder.course;

import common.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author fance
 * @date 18-6-11 下午12:24
 */
public class TreeAndString {
    /**
     * 层次打印这棵二叉树
     * @param root
     * @return
     */
    public List<List<Integer>> printTree(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int curNum = queue.size();
            List<Integer> tmpList = new ArrayList<>();
            for (int i = 0; i < curNum; i++) {
                TreeNode cur = queue.poll();
                tmpList.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            res.add(tmpList);
        }
        return res;
    }

    /**
     * 判断A和B是否互为旋转词
     * 1.sort 2.A + A 看是否包含B
     * @param A
     * @param lena
     * @param B
     * @param lenb
     * @return
     */
    public boolean chkRotationI(String A, int lena, String B, int lenb) {
        if (lena != lenb) {
            return false;
        }
        char[] a = A.toCharArray();
        char[] b = B.toCharArray();
        Arrays.sort(a);
        Arrays.sort(b);
        String resA = String.valueOf(a);
        String resB = String.valueOf(b);
        return resA.equals(resB);
    }
    public boolean chkRotationII(String A, int lena, String B, int lenb) {
        if (lena != lenb) {
            return false;
        }
        String doubleA = A + A;
        return A.contains(B);
    }
}
